//自定义链接跳转
var app = getApp();

var util = require("../utils/util.js");

const customLink = item => {
    var right_arr = item.right_arr ? JSON.parse(item.right_arr) : "";
    var open_type = "navigateTo";
    if (item.href && (item.href.indexOf("index/index") != -1 || item.href.indexOf("classfit/classfit") != -1 || item.href.indexOf("order/order") != -1 && item.href.indexOf("localLifePackage") == -1 || item.href.indexOf("personal/personal") != -1)) {
        open_type = "switchTab";
        if (item.href.indexOf("order/order") != -1) {
            var order_choose_nav = item.href.split("=")[1];
            wx.setStorageSync("order_choose_nav", order_choose_nav);
        }
        app.globalData.link = item.href;
    }
    switch (item.type) {
      case "6":
        if (right_arr.phone) {
            console.log(right_arr.phone);
            wx.makePhoneCall({
                phoneNumber: right_arr.phone
            });
        }
        //电话
                break;

      case "7":
        if (right_arr.location) {
            var map = right_arr.location.split(",");
            var location = util.bMapTransQQMap(parseFloat(map[0]), parseFloat(map[1]));
            wx.openLocation({
                latitude: Number(location.lat),
                longitude: Number(location.lng),
                name: right_arr.address,
                address: right_arr.address
            });
        }
        //导航
                break;

      case "8":
        //客服
        break;

      case "9":
        if (right_arr.appid) {
            wx.navigateToMiniProgram({
                appId: right_arr.appid,
                path: right_arr.other_route
            });
        }
        //小程序跳转
                break;

      case "10":
        if (right_arr.link) {
            if (right_arr.link.indexOf("http") != -1) {
                wx.navigateTo({
                    url: "/place/pages/webView/webView?url=" + encodeURIComponent(right_arr.link)
                });
            } else {
                if (right_arr.link.indexOf("index/index") != -1 || right_arr.link.indexOf("classfit/classfit") != -1 || right_arr.link.indexOf("order/order") != -1 && right_arr.link.indexOf("localLifePackage") == -1 || right_arr.link.indexOf("personal/personal") != -1) {
                    wx.switchTab({
                        url: right_arr.link
                    });
                } else {
                    wx.navigateTo({
                        url: right_arr.link
                    });
                }
            }
        }
        //自定义链接-内部链接和外部需要区分下
                break;

      case "11":
        //小程序页面链接
        break;

      case "12":
        if (item.href) {
            wx.navigateTo({
                url: item.href
            });
        }
        //淘客京东
                break;

      case "13":
        if (item.href) {
            wx.navigateTo({
                url: item.href
            });
        }
        //淘客拼多多 
                break;

      case "21":
        let href = item.href;
        let arr = href.split("=");
        if (arr[1] && arr[1] != 0) {
            wx.navigateTo({
                url: item.href
            });
        } else {
            wx.showModal({
                title: "温馨提示",
                content: "该区域暂无加盟店",
                complete: res => {
                    if (res.cancel) {}
                    if (res.confirm) {}
                }
            });
        }
        //团长小店 
                break;

      default:
        if (item.href && open_type == "switchTab") {
            wx.switchTab({
                url: item.href,
                success: function(e) {
                    let page = getCurrentPages().pop();
                    if (page == undefined || page == null) return;
                    page.onLoad();
                }
            });
        }
        if (item.href && open_type == "navigateTo") {
            wx.navigateTo({
                url: item.href
            });
        }
    }
};

module.exports = customLink;